<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表脚本和自定义</title>
  <script src="./js/jexcel.js"></script>
  <script src="./js/jsuites.js"></script>
  <link rel="stylesheet" href="./css/jsuites.css" type="text/css" />
  <link rel="stylesheet" href="./css/jexcel.css" type="text/css" />
  <!-- Numeral.js 是一个用于格式化和操作数字的 JavaScript 库。数字可以格式化为货币，百分比，时间，甚至是小数，千位，和缩写格式，功能十分强大。支持包括中文在内的17种语言。 -->
  <script src="./js/numeral.min.js"></script>
</head>

<body>
  <h1>设置表样式</h1>
  <h3>通过javascript自定义表行为。</h3>
  <div id="spreadsheet"></div>
</body>
<script>
  var data2 = [
    ['BR', 'Cheese', 1, 3.99],
    ['CA', 'Apples', 0, 1.00],
    ['US', 'Carrots', 1, 0.90],
    ['GB', 'Oranges', 0, 1.20],
    ['CH', 'Chocolats', 1, 0.40],
    ['AR', 'Apples', 1, 1.10],
    ['AR', 'Bananas', 1, 0.30],
    ['BR', 'Oranges', 1, 0.95],
    ['BR', 'Pears', 1, 0.90],
    ['', '', '', '=ROUND(SUM(D1:D8), 2)'],
  ];

  var table2 = jspreadsheet(document.getElementById('spreadsheet'), {
    data: data2,
    columnSorting: false,
    columns: [
      {
        type: 'autocomplete',
        title: 'Country',
        width: '250',
        url: './static/countries.json'
      },
      {
        type: 'autocomplete',
        title: 'Food',
        width: '150',
        source: ['Apples', 'Bananas', 'Carrots', 'Oranges', 'Cheese', 'Kiwi', 'Chocolats', 'Pears']
      },
      {
        type: 'checkbox',
        title: 'Stock',
        width: '100'
      },
      {
        type: 'number',
        title: 'Price',
        width: '100'
      },
    ],
    updateTable: function (instance, cell, col, row, val, label, cellName) {
      // Number formating
      if (col == 3) {
        // Get text
        txt = cell.innerText;
        // Format text
        txt = numeral(txt).format('0,0.00');
        // Update cell value
        cell.innerHTML = '$ ' + txt;
      }

      // Total row
      if (row == 9) {
        if (col < 3) {
          cell.innerHTML = '';
        }

        if (col == 2) {
          cell.innerHTML = 'Total';
          cell.style.fontWeight = 'bold';
        }

        cell.className = '';
        cell.style.backgroundColor = '#f46e42';
        cell.style.color = '#ffffff';
      }
    }
  });
</script>

<style>
  .jexcel tbody tr:nth-child(even) {
    background-color: #EEE9F1 !important;
  }
</style>

</html>